//
//  AlarmSearchVC.swift
//  SBSafetyApp
//
//  Created by Lina on 2023/4/22.
//
// // 【首页-报警通知】报警记录-搜索

import Foundation
import UIKit

class AlarmSearchVC: SubLevelViewController {
    let detailV = AlarmSearchView()

    open var didSelectItemWith:((_ param: SpCommonParam) -> ())?
    
    var start: Date? {
        didSet {
            reloadData()
        }
    }
    
    var end: Date? {
        didSet {
            reloadData()
        }
    }
    
    var dept = DeptModal(deptId: BSUser.currentUser.deptId, deptName: BSUser.currentUser.deptName) {
        didSet {
            reloadData()
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        title = "报警记录查询"
        view.backgroundColor = .bg
        
        setupUI()
        
        reloadData()
    }
    
    func reloadData() {
        detailV.deptItem.value = dept.deptName
        detailV.startDateItem.value = start?.elTodayDate()
        detailV.endDateItem.value = end?.elTodayDate()
    }
    
    @objc func deptSearchTapped() {
        let bSearchVC = BranchSearchVC()
        bSearchVC.didSelectBranchWith = { (dept, _) -> () in
            self.dept = dept
            self.detailV.deptItem.value = dept.deptName
            bSearchVC.navigationController?.popViewController(animated: true)
        }
        navigationController?.pushViewController(bSearchVC, animated: true)
    }
    
    @objc func typeSearchTapped() {
        
    }
    
    @objc func startSearchTapped() {
        self.popup.bottomSheet(bgColor: .black.withAlphaComponent(0.65)) {
            let v = CornerView(frame: CGRect(x: 0, y: 0, width: ScreenWidth, height: 386))
            v.corners = SBRectCorner(topLeft: 12, topRight: 12, bottomLeft: 0, bottomRight: 0)
            v.backgroundColor = .white
            let rPopV = PickupPopView()
            rPopV.dateMode = .date
            rPopV.maximumDate = Date()
            rPopV.date = start
            rPopV.didSelectDateWith = { (picker, isComfirm) -> () in
                if isComfirm {
                    self.start = picker.date
                }
            }
            v.addSubview(rPopV)
            rPopV.snp.makeConstraints { (make) -> Void in
                make.top.right.left.bottom.equalToSuperview()
            }
            return v
        }
    }
    
    @objc func endSearchTapped() {
        self.popup.bottomSheet(bgColor: .black.withAlphaComponent(0.65)) {
            let v = CornerView(frame: CGRect(x: 0, y: 0, width: ScreenWidth, height: 386))
            v.corners = SBRectCorner(topLeft: 12, topRight: 12, bottomLeft: 0, bottomRight: 0)
            v.backgroundColor = .white
            let rPopV = PickupPopView()
            rPopV.dateMode = .date
            rPopV.maximumDate = Date()
            rPopV.date = end
            rPopV.didSelectDateWith = { (picker, isComfirm) -> () in
                if isComfirm {
                    self.end = picker.date
                }
            }
            v.addSubview(rPopV)
            rPopV.snp.makeConstraints { (make) -> Void in
                make.top.right.left.bottom.equalToSuperview()
            }
            return v
        }
    }
    
    @objc func submitTapped() {
        let param = SpCommonParam(rwkssj: start?.elTodayDate(), rwjssj: end?.elTodayDate(), rwlxmc: detailV.taskItem.value)
        didSelectItemWith?(param)
        navigationController?.popViewController(animated: true)
    }
    
    // MARK: - Setup
    
    func setupUI() {
        detailV.deptItem.addTarget(self, action: #selector(deptSearchTapped), for: .touchUpInside)
        detailV.taskItem.addTarget(self, action: #selector(typeSearchTapped), for: .touchUpInside)
        detailV.startDateItem.addTarget(self, action: #selector(startSearchTapped), for: .touchUpInside)
        detailV.endDateItem.addTarget(self, action: #selector(endSearchTapped), for: .touchUpInside)
        detailV.submitBtn.addTarget(self, action: #selector(submitTapped), for: .touchUpInside)
        view.addSubview(detailV)
        detailV.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.top.equalTo(view.safeAreaLayoutGuide.snp.top)
            make.bottom.equalTo(view.safeAreaLayoutGuide.snp.bottom)
        }
    }
}
